Android_检查是否联网 Posted on 2016-05-14 | 检查是否联网 1234567891011121314151617181920212223242526272829303132public static boolean isNetworkAvailable(Activity mActivity) { Context context = mActivity.getApplicationContext(); ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false;}public static void HttpTest(final Activity mActivity) { if (!isNetworkAvailable(mActivity)) { showToast(mActivity); }}public static void showToast(Activity mActivity) { Vibrator mVibrator01 = (Vibrator) mActivity.getApplication() .getSystemService(Service.VIBRATOR_SERVICE); mVibrator01.vibrate(new long[] { 100, 10, 100, 1000 }, -1); Toast.makeText(mActivity, "请检查是否联网", 500).show();} 其中还要加上手机震动和联网的权限: 123<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.VIBRATE"/>